package com.mxpio.erp.hr.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.hr.entity.Employee;
import com.mxpio.erp.common.hr.service.EmployeeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "EmployeeController", description = "人力接口")
@RestController
@RequestMapping("/erp/hr/employee/")
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeSerivce;

	@GetMapping("list")
	@Operation(summary = "员工列表", description = "获取员工列表", method = "GET")
	public Result<List<Employee>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<Employee> tos = employeeSerivce.list(Employee.class, criteria);
		return Result.OK(tos);
	}
	
	@GetMapping("page")
	@Operation(summary = "员工列表", description = "获取员工列表", method = "GET")
	public Result<Page<Employee>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Employee> page = employeeSerivce.listPage(Employee.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{bizNo}")
	@Operation(summary = "根据bizNo获取员工", description = "根据bizNo获取员工", method = "GET")
	public Result<Employee> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
		Employee employee = employeeSerivce.getById(Employee.class, bizNo);
		return Result.OK(employee);
	}
	
	@PostMapping("add")
	@Operation(summary = "新增员工", description = "新增员工", method = "POST")
	public Result<Employee> add(@RequestBody Employee employee) {
		employeeSerivce.save(employee);
		return Result.OK(employee);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑员工", description = "编辑员工（全量）", method = "PUT")
	public Result<Employee> edit(@RequestBody Employee employee) {
		employeeSerivce.update(employee);
		return Result.OK(employee);
	}
	
	@DeleteMapping("remove/{bizNos}")
	@Operation(summary = "删除员工", description = "删除员工", method = "DELETE")
	public Result<Employee> remove(@PathVariable(name = "bizNos", required = true) String bizNos) {
		String bizNo[] = bizNos.split(",");
		for(String key : bizNo){
			employeeSerivce.delete(Employee.class, key);
		}
		return Result.OK();
	}
	
}
